/*
Demonstration of how to create and use multiple windows with
the G4P (GUI for Processing) library.

(c)2012 Peter Lager

A detailed explanation of the code used in this example can be found at
http://www.lagers.org.uk/g4p/applets/g4p_windowsstarter/index.html

 */

package g4p.tests;

import g4p_controls.G4P;
import g4p_controls.GAlign;
import g4p_controls.GButton;
import g4p_controls.GEvent;
import g4p_controls.GLabel;
import g4p_controls.GWinApplet;
import g4p_controls.GWinData;
import g4p_controls.GWindow;

import processing.core.PApplet;
import processing.event.MouseEvent;


public class WindowStarter extends PApplet{

	private GWindow[] window;

	private GButton btnStart;
	private GLabel lblInstr;

	public void setup(){
		size(360,50);
		  size(256, 128);
		  btnStart = new GButton(this, 4, 34, 120, 60, "Create 3 Windows");
		  lblInstr = new GLabel(this, 132, 34, 120, 60, "Use the mouse to draw a rectangle in any of the 3 windows");
		  lblInstr.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
		  lblInstr.setVisible(false);
	}

	/**
	 * Draw for the main window
	 */
	public void draw(){
		background(192);
	}

	public void createWindows(){
		int col;
		window = new GWindow[3];
		for(int i = 0; i < 3; i++){
			col = (128 << (i * 8)) | 0xff000000;
			window[i] = new GWindow(this, "Window "+i, 70+i*220, 160+i*50,200,200, false, JAVA2D);
			window[i].setBackground(col);
			window[i].addData(new MyWinData());
			window[i].addDrawHandler(this, "windowDraw");
			window[i].addMouseHandler(this, "windowMouse");
			window[i].setActionOnClose(G4P.CLOSE_WINDOW);
		}
	}

	/**
	 * Click the button to create the windows.
	 * @param button
	 */
	public void handleButtonEvents(GButton button, GEvent event){
		if(window == null && event == GEvent.CLICKED){
			createWindows();
			lblInstr.setVisible(true);
			button.setEnabled(false);
		}
	}

	/**
	 * Handles mouse events for ALL GWindow objects
	 *  
	 * @param appc the PApplet object embeded into the frame
	 * @param data the data for the GWindow being used
	 * @param event the mouse event
	 */
	public void windowMouse(GWinApplet appc, GWinData data, MouseEvent event){
		MyWinData data2 = (MyWinData)data;
		switch(event.getAction()){
		case MouseEvent.PRESS:
			data2.sx = data2.ex = appc.mouseX;
			data2.sy = data2.ey = appc.mouseY;
			data2.done = false;
			break;
		case MouseEvent.RELEASE:
			data2.ex = appc.mouseX;
			data2.ey = appc.mouseY;
			data2.done = true;
			break;
		case MouseEvent.DRAG:
			data2.ex = appc.mouseX;
			data2.ey = appc.mouseY;
			break;			
		}
	}

	/**
	 * Handles drawing to the windows PApplet area
	 * 
	 * @param appc the PApplet object embeded into the frame
	 * @param data the data for the GWindow being used
	 */
	public void windowDraw(GWinApplet appc, GWinData data){
		MyWinData data2 = (MyWinData)data;
		if(!(data2.sx == data2.ex && data2.ey == data2.ey)){
			appc.stroke(255);
			appc.strokeWeight(2);
			appc.noFill();
			if(data2.done){
				appc.fill(128);
			}
			appc.rectMode(CORNERS);
			appc.rect(data2.sx, data2.sy, data2.ex, data2.ey);
		}
	}	

	/**
	 * Simple class that extends GWinData and holds the data that is specific
	 * to a particular window.
	 * 
	 * @author Peter Lager
	 *
	 */
	class MyWinData extends GWinData {
		public int sx,sy,ex,ey;
		public boolean done;
	}
}
